package com.chhuang.system.controller;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chhuang.system.bean.UserWithRoleIdQueryBean;
import com.chhuang.system.consts.SysConstants;
import com.chhuang.system.po.ChRole;
import com.chhuang.system.po.ChUser;
import com.chhuang.system.po.ChUserRoleMap;
import com.chhuang.system.service.IChRoleService;
import com.chhuang.system.service.IChUserRoleMapService;
import com.chhuang.system.service.IChUserService;
import com.chhuang.utils.Assert;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/system/userRoleMap")
public class ChUserRoleMapController {
	private static final Log log = LogFactory.getLog(ChUserRoleMapController.class);
	@Autowired
	private IChUserService chUserService;
	@Autowired
	private IChRoleService chRoleService;
	@Autowired
	private IChUserRoleMapService chUserRoleMapService;
	
	/**
	 * 获取用户的所有角色信息
	 * @param userId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/getRolesByUserId",method=RequestMethod.POST)
	@ResponseBody
	public Object getRolesByuserId(String userId) throws Exception{
		List<ChRole> roles = null;
		List<ChUserRoleMap> userRoleMaps = chUserRoleMapService.findByUserId(userId);
		if(Assert.isValidCollection(userRoleMaps)){
			String[] roleIds = new String[userRoleMaps.size()];
			for(int i=0;i<userRoleMaps.size();i++){
				roleIds[i] = userRoleMaps.get(i).getRoleId();
			}
			roles = chRoleService.findByRoleIds(roleIds);
			return roles;
		}
		log.debug("获取用户的所有角色信息");
		return SysConstants.Symbol.EMPTY_JSONARRAY;
	}
	
	/**
	 * 
	 * @param pageSize
	 * @param pageNum
	 * @param orderBy
	 * @param desc
	 * @param or
	 * @param searchText
	 * @param roleId
	 * @param isIn
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/getUsers",method=RequestMethod.POST)
	@ResponseBody
	public Object getUsers(@RequestBody UserWithRoleIdQueryBean userWRoleQueryBean) throws Exception{
		log.debug(userWRoleQueryBean.getPageNum()+";"+
				userWRoleQueryBean.getPageSize()+";"+
				userWRoleQueryBean.getOrderBy()+";"+
				userWRoleQueryBean.isDesc()+";"+
				userWRoleQueryBean.getUsername()+";"+
				userWRoleQueryBean.getNickname()+";"+
				userWRoleQueryBean.getPhone()+";"+
				userWRoleQueryBean.getEmail());
		return chUserRoleMapService.findUsersByRoleIdAndQueryConditions(userWRoleQueryBean);
	}
	
	/**
	 * 给角色添加用户
	 * @param roleId
	 * @param userIds
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/roleAddUsers",method=RequestMethod.POST)
	@ResponseBody
	public Object roleAddUsers(String roleId,String userIds) throws Exception{
		JSONObject json = new JSONObject();
		if(Assert.isValidString(roleId) && Assert.isValidString(userIds)){
			ChRole role = chRoleService.findByPrimaryKey(roleId);
			if(role!=null){
				String[] userIdArray = userIds.split(SysConstants.Symbol.COMMA);
				for(String userId : userIdArray){
					ChUser user = chUserService.findByPrimaryKey(userId);
					if(user!=null){
						ChUserRoleMap userRoleMap = new ChUserRoleMap();
						userRoleMap.setRoleId(roleId);
						userRoleMap.setUserId(userId);
						userRoleMap.setRoleOrder(role.getRoleOrder());
						chUserRoleMapService.save(userRoleMap);
						json.put(SysConstants.Status.SUCCESS, true);
					}
				}
			}
		}
		return json;
	}
	
	/**
	 * 根据userId和roleId删除用户与角色的映射关系
	 * @param userId
	 * @param roleId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/delUserRoleMap",method=RequestMethod.POST)
	@ResponseBody
	public Object delUserRoleMap(String roleId,String userIds) throws Exception{
		JSONObject json = new JSONObject();
		if(Assert.isValidString(userIds) && Assert.isValidString(roleId)){
			String[] userIdArray = userIds.split(SysConstants.Symbol.COMMA);
			for(String userId : userIdArray){
				chUserRoleMapService.deleteByUserIdRoleId(userId, roleId);
			}
			json.put(SysConstants.Status.SUCCESS, true);
		}
		return json;
	}
}
